uniform mat4 boneMat[32]; //72/3 == 24 bones. We cant use 4x3mat bcos ATI drivers suck

attribute vec4 boneIds;   //Upto 4 bones per vertex

attribute vec4 boneWeights; //Upto 4 weights for 4 bones



//////////////LIGHTING/////////////////////

vec3 normal, lightDir;

vec4 diffuse, ecPos;

float NdotL;



void main()

{		

	vec3 final = vec3 (0,0,0);

	vec3 finalN = vec3 (0,0,0);

	for(int i = 0; i < 4 ; ++i)

	{

    		int boneID = int(boneIds[i]);//*3 because 1 matrix requires 3 vec4s



		mat4 boneMatrix = boneMat[boneID];

		

		final +=  (gl_Vertex * boneMatrix).xyz * boneWeights[i];

		

		//For normals, we only want the rotation to affect us

		// extract out only the rotational portion of the matrix

		mat3 boneRotMatrix = mat3(boneMatrix[0].xyz, boneMatrix[1].xyz, boneMatrix[2].xyz);

		//mat3 boneRotMatrix = mat3(boneMatrix);

		finalN += (gl_Normal * boneRotMatrix).xyz  * boneWeights[i];



	}



	normal = normalize( gl_NormalMatrix * gl_Normal );//transform normal to eye coords

	



	ecPos = gl_ModelViewMatrix * vec4(final, 1);//transform vertex to eye coords

	lightDir = normalize( vec3(gl_LightSource[0].position - ecPos) );//calc light direction



	NdotL = max(dot(normal, lightDir), 0.0);

	diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;

	

	gl_Position = gl_ModelViewProjectionMatrix * vec4(final,1);
	//gl_Position = ftransform();


	gl_FrontColor = NdotL * diffuse;
		

	gl_TexCoord[0] = gl_MultiTexCoord0;



} 



